﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using DomainDriver.DomainModeling.DataProviders;

namespace DomainDriver.DomainModeling.Configuration
{
    public class DomainDriverConfigurationHelper
    {
        public const string RecommendedGroupName = "DomainDriver";
        public const string RecommendedSectionName = "DomainDriverConfiguration";
        public static readonly DomainDriverConfigurationHelper Current = new DomainDriverConfigurationHelper();

        private bool s_DoSubAssemblyResistration = DomainDriverConfiguration.DoSubAssemblyResistration_DefaultValue;
        private string s_ConnectionToUse = DomainDriverConfiguration.ConnectionToUse_DefaultValue;
        private DataSourcePersistenceType s_PersistenceTypeToUse = DomainDriverConfiguration.PersistenceTypeToUse_DefaultValue;

        public DomainDriverConfigurationHelper()
        {
            string[] sectionPathsToTry = new string[] { (RecommendedGroupName + "/" + RecommendedSectionName), RecommendedSectionName, RecommendedGroupName };

            try
            {
                foreach (string path in sectionPathsToTry)
                {
                    object configurationSection = ConfigurationManager.GetSection(path);

                    if (configurationSection == null)
                    { continue; }

                    DomainDriverConfiguration typedConfigurationSection = (DomainDriverConfiguration)configurationSection;

                    s_DoSubAssemblyResistration = typedConfigurationSection.DoSubAssemblyResistration;
                    s_ConnectionToUse = typedConfigurationSection.ConnectionToUse;
                    s_PersistenceTypeToUse = typedConfigurationSection.PersistenceTypeToUse;
                    return;
                }
            }
            catch (Exception exception)
            {
                s_DoSubAssemblyResistration = DomainDriverConfiguration.DoSubAssemblyResistration_DefaultValue;
                s_ConnectionToUse = DomainDriverConfiguration.ConnectionToUse_DefaultValue;
                s_PersistenceTypeToUse = DomainDriverConfiguration.PersistenceTypeToUse_DefaultValue;
            }
        }

        public bool DoSubAssemblyResistration
        {
            get { return s_DoSubAssemblyResistration; }
        }

        public string ConnectionToUse
        {
            get { return s_ConnectionToUse; }
        }

        public DataSourcePersistenceType PersistenceTypeToUse
        {
            get { return s_PersistenceTypeToUse; }
        }
    }
}